package com.caifu.controller.elderlyResidents;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.caifu.bean.PageDo;
import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.controller.AbstractController;
import com.caifu.excel.Custemhandler;
import com.caifu.excel.ExcelElderlyInfo;
import com.caifu.neums.StayBackReasonEnum;
import com.caifu.pojo.YlAssessmentRecord;
import com.caifu.pojo.YlElderlyInfo;
import com.caifu.pojo.YlNursingRecord;
import com.caifu.service.ElderlyInfoService;
import com.caifu.util.Util;
import com.caifu.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * ClassName : ElderlyInfoController
 *
 * @author :  yb
 * @description ：
 * @date : 2020-11-06 16:52
 */
@Controller
@RequestMapping("/elderlyInfo")
public class ElderlyInfoController extends AbstractController {

    private static final Logger logger = LoggerFactory.getLogger(ElderlyInfoController.class);

    @Resource
    private ElderlyInfoService elderlyInfoService;


    /**
     * 跳转  老人 列表
     *
     * @return Result
     */
    @RequestMapping(value = "jumpElderlyInfoList", method = RequestMethod.GET)
    public ModelAndView jumpElderlyInfoList() {
        ModelAndView modelAndView = new ModelAndView();
        try {
            //楼苑列表
            modelAndView.addObject("buildingList", elderlyInfoService.getBuildingListByPensionNo(getUser().getCurrentPensionNo()));
            modelAndView.setViewName("/elderlyInfo/elderlyInfo_list");
            //页面需要的数据
            modelAndView.addObject("pageData", elderlyInfoService.getAddPageOfElderlyInfoData());

        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return modelAndView;
    }

    /**
     * 获取养老院老人信息列表
     *
     * @return Result
     */
    @GetMapping(value = "getElderlyInfoPageList")
    @ResponseBody
    public Result<Object> getElderlyInfoPageList(ElderlyInfoVO vo) {
        try {
            if (Util.isNull(vo.getCurrentPage()) || Util.isNull(vo.getPageSize())) {
                return ResultUtil.getPageDataSuccess(new PageDo<>(null, null, null, null, null));
            }
            vo.setPensionNo(getUser().getCurrentPensionNo());
            if (vo.getIsStayBack() == 1) {
                vo.setUserState("1");
            } else if (vo.getIsStayBack() == 2) {
                vo.setUserState("8");
            }

            return elderlyInfoService.getElderlyInfoPageList(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 获取 敬老院入住老人明细表
     *
     * @param response response
     */
    @GetMapping(value = "getElderlyInfoReport", headers = "Accept=application/octet-stream")
    public void getElderlyInfoReport(ElderlyInfoVO vo, HttpServletResponse response) {
        try {

            vo.setPensionNo(getUser().getCurrentPensionNo());
            if (vo.getIsStayBack() == 1) {
                vo.setUserState("1");
            } else if (vo.getIsStayBack() == 2) {
                vo.setUserState("8");
            }

            List<ExcelElderlyInfo> excelList = elderlyInfoService.getElderlyInfoReportData(vo);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 可以防止中文乱码
            String fileName = URLEncoder.encode("敬老院入住老人明细表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + "." + ExcelTypeEnum.XLSX);
            EasyExcel.write(response.getOutputStream(), ExcelElderlyInfo.class)
                    .sheet("敬老院入住老人明细表")
                    .registerWriteHandler(new Custemhandler())
                    .doWrite(excelList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取楼层信息
     *
     * @return Result
     */
    @GetMapping(value = "getFloorByBuildingNo")
    @ResponseBody
    public Result<Object> getFloorByBuildingNo(String buildingNo) {
        try {
            return elderlyInfoService.getFloorByBuildingNo(buildingNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 获取房屋信息
     *
     * @return Result
     */
    @GetMapping(value = "getRoomByFloorNo")
    @ResponseBody
    public Result<Object> getRoomByFloorNo(String floorNo) {
        try {
            return elderlyInfoService.getRoomByFloorNo(floorNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 获取床位信息
     *
     * @return Result
     */
    @GetMapping(value = "getBedByRoomNo")
    @ResponseBody
    public Result<Object> getBedByRoomNo(String roomNo) {
        try {
            return elderlyInfoService.getBedByRoomNo(roomNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 验证新增老人  身份证号是否存在
     *
     * @return Result
     */
    @PostMapping(value = "isExistForUserPid")
    @ResponseBody
    public Result<Object> isExistForUserPid(String userPid) {
        try {
            if (Util.isNull(userPid)) {
                return ResultUtil.errorMsg("身份证号不能为空");
            }
            return elderlyInfoService.isExistForUserPid(userPid);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 跳转  新增老人 页面
     *
     * @return Result
     */
    @GetMapping(value = "jumpElderlyInfoAdd")
    public ModelAndView jumpElderlyInfoAdd(String userPid) {
        ModelAndView modelAndView = new ModelAndView();
        try {
            modelAndView.setViewName("/elderlyInfo/elderlyInfo_add");
            modelAndView.addObject("userPid", userPid);

            //页面需要的数据
            modelAndView.addObject("pageData", elderlyInfoService.getAddPageOfElderlyInfoData());
            //楼苑
            modelAndView.addObject("buildingList", elderlyInfoService.getBuildingListByPensionNo(getUser().getCurrentPensionNo()));
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return modelAndView;
    }

    /**
     * 获取护理人员
     */
    @GetMapping(value = "getYlStaffInfoList")
    @ResponseBody
    public Result<Object> getYlStaffInfoList(YlStaffInfoVo ylStaffInfoVo) {
        try {
            System.out.println(getUser().getCurrentPensionNo());
            //放入当前养老院机构
            ylStaffInfoVo.setCurrentPensionNo(getUser().getCurrentPensionNo());
            return elderlyInfoService.getYlStaffInfoList(ylStaffInfoVo);
        } catch (Exception ex) {
            logger.error(ex.toString());
            return ResultUtil.getPageDataSuccess(new PageDo<>(null, null, null, null, null));
        }
    }


    /**
     * 保存老人
     *
     * @return Result
     */
    @PostMapping(value = "saveYlElderlyInfo")
    @ResponseBody
    public Result<Object> saveYlElderlyInfo(@RequestBody ElderlyInfoAddVO elderlyInfoAddVO) {
        try {
            elderlyInfoAddVO.setPensionNo(getUser().getCurrentPensionNo());
            elderlyInfoAddVO.setTellerNo(String.valueOf(getUser().getUserNo()));

            return elderlyInfoService.saveYlElderlyInfo(elderlyInfoAddVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 删除老人
     *
     * @return Result
     */
    @PostMapping(value = "deleteYlElderlyInfo")
    @ResponseBody
    public Result<Object> deleteYlElderlyInfo(String userNo) {
        try {
            return elderlyInfoService.deleteYlElderlyInfo(userNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 跳转  老人 查看 页面
     *
     * @return Result
     */
    @RequestMapping(value = "jumpElderlyInfoDetail", method = RequestMethod.GET)
    public ModelAndView jumpElderlyInfoDetail(String userNo) {
        ModelAndView modelAndView = new ModelAndView();
        try {
            //老人基本信息
            modelAndView.addObject("elderlyInfoOfBase", elderlyInfoService.getElderlyInfoDetailByUserNo(userNo));
            //楼苑列表
            modelAndView.addObject("buildingList", elderlyInfoService.getBuildingListByPensionNo(getUser().getCurrentPensionNo()));
            //页面需要的数据
            modelAndView.addObject("pageData", elderlyInfoService.getAddPageOfElderlyInfoData());
            //退住理由
            modelAndView.addObject("stayBackReason", StayBackReasonEnum.getStayBackReasonList());
            modelAndView.setViewName("/elderlyInfo/elderlyInfo_detail");
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return modelAndView;
    }


    /**
     * 跳转  老人 修改 页面
     *
     * @return Result
     */
    @RequestMapping(value = "jumpElderlyInfoUpdate", method = RequestMethod.GET)
    public ModelAndView jumpElderlyInfoUpdate(String userNo) {
        ModelAndView modelAndView = new ModelAndView();
        try {
            //页面需要的数据
            modelAndView.addObject("pageData", elderlyInfoService.getAddPageOfElderlyInfoData());
            modelAndView.addObject("userNo", userNo);
            modelAndView.setViewName("/elderlyInfo/elderlyInfo_update");
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return modelAndView;
    }


    /**
     * 获取老人信息
     *
     * @return Result
     */
    @PostMapping(value = "getElderlyInfoByUserNo")
    @ResponseBody
    public Result<Object> getElderlyInfoByUserNo(String userNo) {
        try {
            return ResultUtil.getDataSuccess(elderlyInfoService.getElderlyInfoByUserNo(userNo));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 修改 老人信息
     *
     * @return Result
     */
    @PostMapping(value = "updateYlElderlyInfoByUserNo")
    @ResponseBody
    public Result<Object> updateYlElderlyInfoByUserNo(@RequestBody ElderlyInfoUpdateVO elderlyInfoUpdateVO) {
        try {
            elderlyInfoUpdateVO.setPensionNo(getUser().getCurrentPensionNo());
            elderlyInfoUpdateVO.setTellerNo(String.valueOf(getUser().getUserNo()));

            return elderlyInfoService.updateYlElderlyInfoByUserNo(elderlyInfoUpdateVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 修改 老人 费用信息
     *
     * @return Result
     */
    @PostMapping(value = "updateYlElderlyInfoFee")
    @ResponseBody
    public Result<Object> updateYlElderlyInfoFee(YlElderlyInfo elderlyInfo) {
        try {
            return elderlyInfoService.updateYlElderlyInfoFee(elderlyInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 修改 老人 入驻合同
     *
     * @return Result
     */
    @PostMapping(value = "updateYlElderlyInfoContract")
    @ResponseBody
    public Result<Object> updateYlElderlyInfoContract(ElderlyInfoUpdateContractVO infoUpdateContractVO) {
        try {
            infoUpdateContractVO.setTellerNo(String.valueOf(getUser().getUserNo()));
            return elderlyInfoService.updateYlElderlyInfoContract(infoUpdateContractVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 修改 老人 入驻合同   -----续签合同
     *
     * @return Result
     */
    @PostMapping(value = "updateYlElderlyInfoContractOfRenewal")
    @ResponseBody
    public Result<Object> updateYlElderlyInfoContractOfRenewal(ElderlyInfoUpdateContractVO infoUpdateContractVO) {
        try {
            infoUpdateContractVO.setTellerNo(String.valueOf(getUser().getUserNo()));
            return elderlyInfoService.updateYlElderlyInfoContractOfRenewal(infoUpdateContractVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 修改 老人 房间信息
     *
     * @return Result
     */
    @PostMapping(value = "updateYlElderlyInfoBuildingRoomBed")
    @ResponseBody
    public Result<Object> updateYlElderlyInfoBuildingRoomBed(ElderlyInfoUpdateBuildingRoomBedVO infoUpdateBuildingRoomBedVO) {
        try {
            infoUpdateBuildingRoomBedVO.setTellerNo(String.valueOf(getUser().getUserNo()));
            infoUpdateBuildingRoomBedVO.setPensionNo(getUser().getCurrentPensionNo());
            return elderlyInfoService.updateYlElderlyInfoBuildingRoomBed(infoUpdateBuildingRoomBedVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 修改 老人 护理等级
     *
     * @return Result
     */
    @PostMapping(value = "updateYlElderlyInfoNursing")
    @ResponseBody
    public Result<Object> updateYlElderlyInfoNursing(YlNursingRecord nursingRecord) {
        try {
            nursingRecord.setTellerNo(String.valueOf(getUser().getUserNo()));

            return elderlyInfoService.updateYlElderlyInfoNursing(nursingRecord);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     * 修改 老人 长护险
     *
     * @return Result
     */
    @PostMapping(value = "updateYlElderlyInfoAssessment")
    @ResponseBody
    public Result<Object> updateYlElderlyInfoAssessment(YlAssessmentRecord assessmentRecord) {
        try {
            assessmentRecord.setTellerNo(String.valueOf(getUser().getUserNo()));

            return elderlyInfoService.updateYlElderlyInfoAssessment(assessmentRecord);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 老人 退住
     */
    @PostMapping(value = "updateElderlyForIsStayBack")
    @ResponseBody
    public Result<Object> updateElderlyForIsStayBack(YlElderlyInfo elderlyInfo) {
        try {
            elderlyInfo.setTellerNo(String.valueOf(getUser().getUserNo()));
            return elderlyInfoService.updateElderlyForIsStayBack(elderlyInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 老人 入驻合同 历史记录查询
     */
    @PostMapping(value = "getHistoryContractList")
    @ResponseBody
    public Result<Object> getHistoryContractList(String userNo) {
        try {
            return elderlyInfoService.getHistoryContractList(userNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     *   获取床位记录 历史
     */
    @PostMapping(value = "getHistoryBedRecordList")
    @ResponseBody
    public Result<Object> getHistoryBedRecordList(String userNo) {
        try {
            return elderlyInfoService.getHistoryBedRecordList(userNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     *   获取护理等级 记录 历史
     */
    @PostMapping(value = "getHistoryNursingRecordList")
    @ResponseBody
    public Result<Object> getHistoryNursingRecordList(String userNo) {
        try {
            return elderlyInfoService.getHistoryNursingRecordList(userNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     *   长护险评估信息历史记录
     */
    @PostMapping(value = "getHistoryAssessmentRecordList")
    @ResponseBody
    public Result<Object> getHistoryAssessmentRecordList(String userNo) {
        try {
            return elderlyInfoService.getHistoryAssessmentRecordList(userNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


    /**
     *   获取 YlCost
     */
    @PostMapping(value = "getCost")
    @ResponseBody
    public Result<Object> getCost(String costNo) {
        try {
            return elderlyInfoService.getCost(costNo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }












}
